# MICROC - A miniature curses alternative
import textwrap as tw
import termios, fcntl, sys, os

ESC = chr(0x1B)

# Color Constants
CBLACK = 0
CDRED = 1
CDYELLOW = 2
CDGREEN = 3
CDCYAN = 4
CDBLUE = 5
CDMAGENTA = 6
CLGRAY = 7
CDGRAY = 8
CRED = 9
CYELLOW = 10
CGREEN = 11
CCYAN = 12
CBLUE = 13
CMAGENTA = 14
CWHITE = 15
CDEFAULT = 16

# Color Tables
FG = (
  30, 31, 33, 32, 36, 34, 35, 37,
  90, 91, 93, 92, 96, 94, 95, 97, 97 )
BG = (
  40, 41, 43, 42, 46, 44, 45, 47,
  100,101,103,102,106,104,105,107, 40 )

def doautoscroll(yes=True):
  if yes:
    print(ESC+"[?7h", end="")
  else:
    print(ESC+"[?7l", end="")

def doshowcsr(yes=True):
  if yes:
    print(ESC+"[?25h", end="")
  else:
    print(ESC+"[?25l", end="")

def clrscrn(fg=CWHITE, bg=CBLACK):
  print(ESC+"[0;"+str(FG[fg])+";"+str(BG[bg])+"m"+ESC+"[2J"+ESC+"[H", end="")

def setcsr(col, row):
  print(ESC+"["+str(row)+";"+str(col)+"H", end="")

def cprint(s, fg=CWHITE, bg=CBLACK):
  print(ESC+"["+str(FG[fg])+";"+str(BG[bg])+"m"+s, end="")

def cprints(seq):
  for elt in seq:
    if type(elt) == type(""):
      print(elt, end="")
    elif type(elt) == type((0,0)):
      cprint("", elt[0], elt[1])

def showdialog(col, row, width, height, txt, bordercol, fg, bg):
  # First, Draw border.
  cprint("", CDEFAULT, bordercol)
  x = col
  y = row
  while y < row+height:
    x = col
    while x < col+width:
      setcsr(x, y)
      print(" ", end="")
      x += 1
    y += 1
  
  # Second, Draw BG rectangle
  cprint("", fg, bg)
  x = col + 1
  y = row + 1
  while y < row+height - 1:
    x = col + 1
    while x < col+width - 1:
      setcsr(x, y)
      print(" ", end="")
      x += 1
    y += 1
  
  # Third, Draw txt
  lines = tw.wrap(txt, width=width-2)
  idx = 0
  while idx < min(len(lines), height-2):
    setcsr(col+1, row+1+idx)
    print(lines[idx], end="")
    idx += 1

# Setup single character input mode

fd = None
flags_save = None
attrs_save = None

def enablesci():
  fd = sys.stdin.fileno()
  # save old state
  flags_save = fcntl.fcntl(fd, fcntl.F_GETFL)
  attrs_save = termios.tcgetattr(fd)
  # make raw - the way to do this comes
  # from the termios(3) man page.
  attrs = list(attrs_save) # copy the stored version to update
  # iflag
  attrs[0] &= ~(termios.IGNBRK | termios.BRKINT |
    termios.PARMRK | termios.ISTRIP | termios.INLCR |
    termios.IGNCR | termios.ICRNL | termios.IXON)
  # oflag
  attrs[1] &= ~termios.OPOST
  # cflag
  attrs[2] &= ~(termios.CSIZE | termios.PARENB)
  attrs[2] |= termios.CS8
  # lflag
  attrs[3] &= ~(termios.ECHONL | termios.ECHO |
    termios.ICANON | termios.ISIG | termios.IEXTEN)
  termios.tcsetattr(fd, termios.TCSANOW, attrs)
  # turn off non-blocking
  fcntl.fcntl(fd, fcntl.F_SETFL, flags_save &
    ~os.O_NONBLOCK)

def getch():
  # read a single keystroke
  ret = None
  try:
    ret = sys.stdin.read(1) # Returns a single character
  except KeyboardInterrupt:
    ret = 0
  return ret

def disablesci():
  # restore old state
  termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save)
  fcntl.fcntl(fd, fcntl.F_SETFL, flags_save)
